package com.pfsoft.grpcclient.controller;

import com.pfsoft.grpc.csv.api.CsvDataServiceGrpc;
import net.devh.boot.grpc.client.inject.GrpcClient;
import com.pfsoft.grpc.csv.api.DataInfo;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * @author pengfei
 * @date 2022年07月20日 11:20
 */
@Slf4j
@RestController
public class GetCsvDataController {

    @GrpcClient("grpc-server-side")
    private CsvDataServiceGrpc.CsvDataServiceBlockingStub csvDataServiceBlockingStub;

    @RequestMapping("/csv")
    public String printMessage(@RequestParam(defaultValue = "will") String name) {
        com.google.protobuf.Empty request =com.google.protobuf.Empty.newBuilder().build();
        Iterator<DataInfo> dataInfoIterator = csvDataServiceBlockingStub.getCsvData(request);
        while (dataInfoIterator.hasNext()) {
            StringBuilder sb=new StringBuilder(10);
            DataInfo datainfo = dataInfoIterator.next();
            sb.append(datainfo.getId()+"|"+datainfo.getDiagnosis()+"|"+datainfo.getRadiusMean()+"|"+datainfo.getTextureMean());
            log.info(sb.toString());
        }
        return "complete";
    }
}
